define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0214_FService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater){
            var service = this;
            service.genController = function(options, controller){
                controller.typeItem = {};
                controller.typeCountMap = {};
                controller.init = function(){
                    $MMWService.PNIGList = {};
                    controller.typeList = [];
                    controller.unselectList = [];
                    controller.filterItem = {query_input:'', queryUnselect:''};
                    controller.selectList = options.machine_list.slice();
                    genSelectMap(controller.selectList);
                    //searchSelectList();
                    search();
                };

                controller.confirm = function(){
                    if(controller.selectList.length > 0){
                        var content = {
                            master_no:options.master_no,
                            master_type : '0',
                            favourite_machine_detail : controller.selectList
                        };
                        favourite_machine_process(content, function(data){
                            $rootScope.changeProgram(options.next, {machine_list: data.query_restult});
                        });
                    } else {
                        $rootScope.showAlert($filter('translate')('B0214_F.msg.selectMachineZero'));
                    }
                };

                controller.searchEquipment = function(event){
                    if(event == undefined || event.keyCode == 13){
                        search(function(){
                            controller.filterItem.queryUnselect = controller.filterItem.query_input;
                        });
                    }
                };

                controller.clickTypeItem = function(index, item){
                    controller.typeItem = item;
                    var showMachines = !controller.typeItem.showMachines;
                    if(showMachines){
                        searchUnselectList(controller.typeItem, function(){
                            controller.typeItem.showMachines = showMachines;
                        });
                    } else {
                        controller.typeItem.showMachines = showMachines;
                    }
                };

                controller.filterUnselect = function(item){
                    return (controller.filterItem.queryUnselect =='' || item.page_no.indexOf(controller.filterItem.queryUnselect) !=-1) && !controller.selectMap[item.page_no];
                };

                controller.clickUnselectItem = function(index, item, typeItem){
                    for(var i in controller.selectList){
                        if(controller.selectList[i].machine_no == item.page_no){
                            $rootScope.showAlert($filter('translate')('B0214_F.msg.machineSelected'));
                            return false;
                        }
                    }
                    controller.selectList.push({machine_no:item.page_no,machine_name:item.page_name, machine_type:typeItem.page_no});
                    //20180416 modify by Dustdusk for mantis#0045371:
                    controller.selectMap[item.page_no] = true;
                    if(!controller.typeCountMap[typeItem.page_no])
                        controller.typeCountMap[typeItem.page_no] = 0;
                    controller.typeCountMap[typeItem.page_no]++;
                };

                controller.clickselectItem = function(index, item){
                    controller.selectList.splice(index, 1);
                    //20180416 modify by Dustdusk for mantis#0045371:
                    delete controller.selectMap[item.machine_no];
                    controller.typeCountMap[item.machine_type]--;
                };

                 //取得設備類別
                function search(callback) {
                    var content = {
                        page_mode : '11',
                        query_condition_info:[]
                    };
                    if(controller.filterItem.query_input!=''){
                        content.query_condition_info.push({column_name:'B.EquipmentNo', query_mode:2,value:'%'+controller.filterItem.query_input+'%',merge_condition_model:'AND'});
                    }
                    $MMWService.pageNumberInfoGet(content, function(itemList){
                        if(itemList && itemList.length > 0){
                            controller.typeList = itemList;
                            genTypeCountMap(controller.typeList);
                            if(controller.typeList.length>0){
                                controller.typeItem = controller.typeList[0];
                                controller.typeItem.showMachines = true;
                                searchUnselectList(controller.typeItem, callback);
                            }
                        } else {
                            // 查無設備類別，請重新確認是否簽核
                            $rootScope.showAlert($filter('translate')('B0214_F.msg.machineTypeNotFound'));
                        }
                    });
                }

                function searchUnselectList(typeItem, callback){
                    $MMWService.pageNumberInfoGet({
                        page_mode : '2',
                        query_condition_info:[
                            {column_name:'EquipmentType', query_mode:'0', value: typeItem.page_no, merge_condition_model:'AND'},
                        ]
                    }, function(itemList){
                        //controller.unselectList = itemList;
                        typeItem.unselectList = itemList;
                        if(callback)
                            callback();
                    });
                }

                //20180416 modify by Dustdusk for mantis#0045371:
                function genSelectMap(selectList){
                    controller.selectMap = {};
                    if(selectList && selectList.length>0){
                        selectList.forEach(function(selectData){
                            controller.selectMap[selectData.machine_no] = true;

                            if(!controller.typeCountMap[selectData.machine_type])
                                controller.typeCountMap[selectData.machine_type] = 0;

                            controller.typeCountMap[selectData.machine_type]++;
                        });
                    }
                }

                //20180417 modify by Dustdusk for mantis#0045371:
                //20181105 modify by Dustdusk for M#: 修正數量計算錯誤
                function genTypeCountMap(typeList){
                    //controller.typeCountMap = {};
                    if(typeList && typeList.length >0){
                        typeList.forEach(function(typeData){
                            if(!controller.typeCountMap[typeData.page_no])
                                controller.typeCountMap[typeData.page_no] = 0;
                        });
                    }
                }


                return controller;
            };

            service.init = function(options){
				$mdDialog.dialog(
					'module/moduleB/program/B02/B0214_F/B0214_F.tmp.html',
					function(dialog){
						var controller = service.genController(options, dialog);
						controller.css_url = 'module/moduleB/program/B02/B0214_F/B0214_F.css';
						controller.beforeShown = function(){
							dialog.init();
						};
						controller.back = function () {
							dialog.hide();
						};
						controller.dialog_confirm = function () {
                            if(controller.selectList.length > 0){
                                var content = {
                                    master_no:options.master_no,
                                    master_type : '0',
                                    favourite_machine_detail : controller.selectList
                                };
                                favourite_machine_process(content, function(data){
                                    if(options.feedback)
                                        options.feedback(dialog,data);
                                });
                            } else {
                                $rootScope.showAlert($filter('translate')('B0214_F.msg.selectMachineZero'));
                            }
						};
						return controller;
					}
				);
            };

            function favourite_machine_info_get(master_no, feedback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_SYS.favourite_machine_info_get',
                    content : {
                        master_no: master_no,
                        master_type:'0',
                        get_employee_data:'Y'
                    },
                    success : function(data){
                        if(feedback)
                            feedback(data);
                    }
                });
            }

            function favourite_machine_process(content, feedback){
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_SYS.favourite_machine_process',
                    content : content,
                    success : function(data){
                        favourite_machine_info_get(content.master_no, function(data){
                            //重新查詢
                            //20180416 modify by 李栋 for mantis#0045407
                            $rootScope.showToast($filter('translate')('B0214_F.msg.saveSucess'));
                            if(feedback)
                                feedback(data);
                        });
                    }
                });
            }
        }]);
});
